package com.emr.dao.impl;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.emr.common.SpringPageBean;
import com.emr.dao.ILoginDao;
import com.emr.vo.MessageVo;
import com.emr.vo.User;

@Repository
public class LoginDAOImpl implements ILoginDao {
	
	@Resource
	protected JdbcTemplate jdbcTemplate;
	private int totalNum;
	private int totalPage;

	@Override
	public MessageVo delete(User user) {
		MessageVo msgVo = new MessageVo();
		try {
			String sql = "delete from bl_user where id=?";
			jdbcTemplate.update(sql, new Object[] { user.getId() });
			msgVo.setSuccess(true);
			msgVo.setMessage("删除数据成功!");
		} catch (Exception e) {
			msgVo.setSuccess(false);
			msgVo.setMessage("删除数据失败！");
			e.printStackTrace();
		}
		return msgVo;
	}
	
	@Override
	public MessageVo save(User user) {
		MessageVo msgVo = new MessageVo();
		try {
			String sql = "insert into bl_user(id, name, password) values(seq_bl_user.nextval, ?, ?)";
			jdbcTemplate.update(sql, new Object[] { user.getName(), user.getPassword() });
			msgVo.setSuccess(true);
			msgVo.setMessage("新增数据成功!");
		} catch (Exception e) {
			msgVo.setSuccess(false);
			msgVo.setMessage("新增数据失败！");
			e.printStackTrace();
		}
		return msgVo;
	}
	
	@Override
	public MessageVo update(User user) {
		MessageVo msgVo = new MessageVo();
		try {
			String sql = "update bl_user set name=?, password=? wehre id=?";
			jdbcTemplate.update(sql, new Object[] { user.getName(), user.getPassword(), user.getId() });
			msgVo.setSuccess(true);
			msgVo.setMessage("更新数据成功!");
		} catch (Exception e) {
			msgVo.setSuccess(false);
			msgVo.setMessage("更新数据失败！");
			e.printStackTrace();
		}
		return msgVo;
	}
	
	@Override
	public MessageVo executeSql(String sql, Object[] obj) {
		MessageVo msgVo = new MessageVo();
		try {
			jdbcTemplate.update(sql, obj);
			msgVo.setSuccess(true);
			msgVo.setMessage("执行语句成功!");
		} catch (Exception e) {
			msgVo.setSuccess(false);
			msgVo.setMessage("执行语句失败！");
			e.printStackTrace();
		}
		return msgVo;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<User> getList(User user, int pageNum, int pageSize) {
		User vo = null;
		String sQuery = "select * from bl_user where 1=1 "; // 查询语句
		String sCount = "select count(*) from bl_user where 1=1 "; // 分页语句
		List<User> list = new ArrayList<User>();
		try {
			// 设置搜索条件
			Object[] sParArr = null;
			ArrayList<Object[]> parList = new ArrayList<Object[]>();
			sParArr = new Object[] { " and name = ? ", user.getName() };
			parList.add(sParArr);
			sParArr = new Object[] { " and password = ? ", user.getPassword() };
			parList.add(sParArr);
			// 将参数传入到分页Bean中
			String[] sHqlArr = { sQuery, sCount, "", "" };
			int[] iInfoArr = { pageNum, pageSize };
			SpringPageBean sp = new SpringPageBean(sHqlArr, iInfoArr, parList);
			Iterator it = sp.doInJdbcTemplate(jdbcTemplate).iterator();
			while (it.hasNext()) {
				Map m = (Map) it.next();
				vo = new User();
				vo.setId(new Integer(m.get("id").toString()));
				vo.setName(m.get("name").toString());
				vo.setPassword(m.get("password").toString());
				list.add(vo);
			}
			this.setTotalNum(sp.getTotalNum());
			this.setTotalPage(sp.getTotalPage());
			// 清空参数
			parList.clear();
			parList = null;
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 返回搜索结果
		return list;
	}

	@Override
	@SuppressWarnings("unchecked")
	public User getOne(User user) {
		User vo = null;
		String sData = "select * from bl_user where 1=1 "; // 查询语句
		try {
			// 设置搜索条件
			Object[] sParArr = null;
			ArrayList<Object[]> parList = new ArrayList<Object[]>();
			sParArr = new Object[] { " and name = ? ", user.getName() };
			parList.add(sParArr);
			sParArr = new Object[] { " and password = ? ", user.getPassword() };
			parList.add(sParArr);
			// 将参数传入到分页Bean中
			String[] sHqlArr = { sData, null, "", "" };
			int[] iInfoArr = { 1, 1 };
			SpringPageBean sp = new SpringPageBean(sHqlArr, iInfoArr, parList);
			// 搜索数据
			Iterator it = sp.doInJdbcTemplate(jdbcTemplate).iterator();
			if (it.hasNext()) {
				vo = new User();
				Map m = (Map) it.next();
				vo.setId(new Integer(m.get("id").toString()));
				vo.setName(m.get("name").toString());
				vo.setPassword(m.get("password").toString());
			}
			// 清空参数
			parList.clear();
			parList = null;
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 返回搜索结果
		return vo;
	}

	@Override
	public int getTotalNum() {
		return totalNum;
	}

	@Override
	public void setTotalNum(int totalNum) {
		this.totalNum = totalNum;
	}

	@Override
	public int getTotalPage() {
		return totalPage;
	}
	
	@Override
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

}
